package top.lyc829.leetcode.T169;

import java.util.HashMap;
import java.util.Map;


class Solution {
    public int majorityElement(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();

        // 将x和x出现的次数映射到map中
        for (int i : nums) {
            map.merge(i, 1, Integer::sum);
        }

        Map.Entry<Integer, Integer> major = null;

        // Map.Entry: 取出map的键值对
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (major == null || entry.getValue() > major.getValue()) {
                major = entry;
            }
        }
        return major != null ? major.getKey() : 0;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int res = s.majorityElement(new int[]{3, 2, 3});
        System.out.println(res);
    }

}
